package org.krivtsun.scanlab.batch;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

/**
 * @author Andrew Krivtsun
 */
class PageCellRenderer extends JLabel implements ListCellRenderer {

    private static final int OUTER_CELL_GAP = 10;
    private static final int BORDER_THICKNESS = 1;
    private static final int INNER_CELL_GAP = 2;

    private static final int THUMBNAIL_WIDTH = 70;
    private static final int THUMBNAIL_HEIGHT = 96;

    private static final int SMALL_IMAGE_WIDTH = THUMBNAIL_WIDTH - 2*BORDER_THICKNESS - 2*INNER_CELL_GAP;
    private static final int SMALL_IMAGE_HEIGHT = THUMBNAIL_HEIGHT - 2*BORDER_THICKNESS - 2*INNER_CELL_GAP;

    private static Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(
            BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS);
    private static Border GAP_BORDER = BorderFactory.createEmptyBorder(
            OUTER_CELL_GAP, OUTER_CELL_GAP, OUTER_CELL_GAP, OUTER_CELL_GAP);

    PageCellRenderer() {
        setOpaque(true);

        setHorizontalAlignment(JLabel.CENTER);
        setVerticalAlignment(JLabel.CENTER);

        setVerticalTextPosition(JLabel.BOTTOM);
        setHorizontalTextPosition(JLabel.CENTER);
    }

    public Component getListCellRendererComponent(
                               JList list,
                               Object value,
                               int index,
                               boolean isSelected,
                               boolean cellHasFocus) {

        PageEntry entry = ((PageEntry)value);

        setComponentOrientation(list.getComponentOrientation());
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        }
        else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        setText(entry.getFile().getName());
        if (entry.getThumbnail() == null) {
            entry.setThumbnail(createThumbnail(entry.getImage()));
        }
        setIcon(new ImageIcon(entry.getThumbnail()));

        setEnabled(list.isEnabled());
        setFont(list.getFont());

        Border border = null;
        if (cellHasFocus) {
            if (isSelected) {
                border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("List.focusCellHighlightBorder");
            }
        } else {
            border = NO_FOCUS_BORDER;
        }
	    setBorder(BorderFactory.createCompoundBorder(border, GAP_BORDER));

        return this;
    }

    static int getCellWidth() {
        return BORDER_THICKNESS + OUTER_CELL_GAP + THUMBNAIL_WIDTH + OUTER_CELL_GAP + BORDER_THICKNESS;
    }

    private Image createThumbnail(RenderedImage image) {
        // create small image
        double width = SMALL_IMAGE_WIDTH;
        double height = SMALL_IMAGE_HEIGHT;
        double widthRatio = width /image.getWidth();
        double heightRatio = height /image.getHeight();
        int thumbWidth, thumbHeight;
        if (heightRatio > widthRatio) {
            thumbWidth = SMALL_IMAGE_WIDTH;
            thumbHeight = (int)(image.getHeight() * widthRatio);
        }
        else {
            thumbWidth = (int)(image.getWidth() * heightRatio);
            thumbHeight = SMALL_IMAGE_HEIGHT;
        }
//        RenderedImage smallImage = image.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);
//
//        // write small image into thumbnail image area
        BufferedImage thumbnail = new BufferedImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
//        Graphics g = thumbnail.getGraphics();
//        g.setColor(Color.black);
//        g.drawRect(0,0, thumbnail.getWidth()-1, thumbnail.getHeight()-1);
//        g.drawImage(smallImage, (thumbnail.getWidth()-thumbWidth)/2,
//                                (thumbnail.getHeight()-thumbHeight)/2, null);
        //return thumbnail;
        return Toolkit.getDefaultToolkit().createImage(thumbnail.getSource());
    }
}
